﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace CPPEI.ETL.Infrastructure
{
    public class HttpWebHelper
    {
        public static string Get(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            return reader.ReadToEnd();
        }

        public static string Post(string url, string json)
        {
            string contentType = "application/x-www-form-urlencoded";
            byte[] postData = Encoding.UTF8.GetBytes(json);
            var content = Post(url, contentType, postData);
            return content;
        }

        public static string Post(string url, string contentType, byte[] postData)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = contentType;
            request.ContentLength = postData.Length;

            Stream stream = request.GetRequestStream();
            stream.Write(postData, 0, postData.Length);
            stream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            return reader.ReadToEnd();
        }
    }
}
